package com.ruoyi.admin.service.impl;

import com.ruoyi.admin.domain.TbBusinessInfo;
import com.ruoyi.admin.domain.TbBusinessInfoVo;
import com.ruoyi.admin.mapper.TbBusinessInfoMapper;
import com.ruoyi.admin.service.ITbBusinessInfoService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务需求Service业务层处理
 * 
 * @author ab
 * @date 2025-07-02
 */
@Service
public class TbBusinessInfoServiceImpl implements ITbBusinessInfoService 
{
    @Autowired
    private TbBusinessInfoMapper tbBusinessInfoMapper;

    /**
     * 查询业务需求
     * 
     * @param id 业务需求主键
     * @return 业务需求
     */
    @Override
    public TbBusinessInfo selectTbBusinessInfoById(Long id)
    {
        return tbBusinessInfoMapper.selectTbBusinessInfoById(id);
    }

    /**
     * 查询业务需求列表
     * 
     * @param tbBusinessInfo 业务需求
     * @return 业务需求
     */
    @Override
    public List<TbBusinessInfoVo> selectTbBusinessInfoList(TbBusinessInfo tbBusinessInfo)
    {
        // TODO 查询列表需管理员需要能看见拥有者
        setUserId(tbBusinessInfo);
        //寻找匹配房源信息
        return tbBusinessInfoMapper.selectBusinessMatchList(tbBusinessInfo);
    }

    /**
     * 新增业务需求
     * 
     * @param tbBusinessInfo 业务需求
     * @return 结果
     */
    @Override
    public int insertTbBusinessInfo(TbBusinessInfo tbBusinessInfo)
    {
        setUserId(tbBusinessInfo);
        return tbBusinessInfoMapper.insertTbBusinessInfo(tbBusinessInfo);
    }

    /**
     * 修改业务需求
     * 
     * @param tbBusinessInfo 业务需求
     * @return 结果
     */
    @Override
    public int updateTbBusinessInfo(TbBusinessInfo tbBusinessInfo)
    {
        return tbBusinessInfoMapper.updateTbBusinessInfo(tbBusinessInfo);
    }

    /**
     * 批量删除业务需求
     * 
     * @param ids 需要删除的业务需求主键
     * @return 结果
     */
    @Override
    public int deleteTbBusinessInfoByIds(Long[] ids)
    {
        return tbBusinessInfoMapper.deleteTbBusinessInfoByIds(ids);
    }

    /**
     * 删除业务需求信息
     * 
     * @param id 业务需求主键
     * @return 结果
     */
    @Override
    public int deleteTbBusinessInfoById(Long id)
    {
        return tbBusinessInfoMapper.deleteTbBusinessInfoById(id);
    }

    private void setUserId(TbBusinessInfo tbBusinessInfo){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(!loginUser.getUser().getRoles().stream().anyMatch(item->item.getRoleKey().equals("admin"))){
            Long userId = loginUser.getUserId();
            tbBusinessInfo.setUserId(userId);
        }
    }
}
